﻿using System;
using System.IO;
using Microsoft.SharePoint;

namespace SKN
{
    /// <summary>
    /// Contains common method for Web
    /// </summary>
    public static class Web
    {
        /// <summary>
        /// Use this function to set Branding options for the web
        /// </summary>
        /// <param name="web"></param>
        /// <param name="siteLogoUrl"></param>
        /// <param name="customMasterUrl"></param>
        /// <param name="alternateCssUrl"></param>
        /// <param name="theme"></param>
        public static void SetBrandingOptions(SPWeb web, string siteLogoUrl, string customMasterUrl,
                                              string alternateCssUrl, string theme)
        {
            if (!string.IsNullOrEmpty(siteLogoUrl))
            {
                web.SiteLogoUrl = siteLogoUrl;
            }

            if (!string.IsNullOrEmpty(theme))
            {
                web.ApplyTheme(theme);
            }

            if (!string.IsNullOrEmpty(customMasterUrl))
            {
                web.CustomMasterUrl = customMasterUrl;
            }

            if (!string.IsNullOrEmpty(alternateCssUrl))
            {
                web.AlternateCssUrl = alternateCssUrl;
            }

            web.Update();
        }

        /// <summary>
        /// Use this function to set welcome page for the web
        /// </summary>
        /// <param name="web"></param>
        /// <param name="welcomePage"></param>
        public static void SetWelcomePage(SPWeb web, string welcomePage)
        {
            if (null != welcomePage.ToLower())
            {
                web.RootFolder.WelcomePage = welcomePage;
                web.RootFolder.Update();
            }
        }

        /// <summary>
        /// Use this method to Add a new web 
        /// </summary>
        /// <param name="web"></param>
        /// <param name="webRelativeUrl"></param>
        /// <param name="webTitle"></param>
        /// <param name="webDesc"></param>
        /// <param name="webTemplate"></param>
        /// <param name="uniquePermission"></param>
        public static void AddWeb(SPWeb web, string webRelativeUrl, string webTitle, string webDesc, string webTemplate,
                                  bool uniquePermission)
        {
            SPWeb newWeb = web.Webs.Add(webRelativeUrl, webTitle, webDesc, 1033, webTemplate, uniquePermission, false);
            newWeb.Dispose();
        }

        /// <summary>
        ///Use this method to Add a Blank web 
        /// </summary>
        /// <param name="web"></param>
        /// <param name="webRelativeUrl"></param>
        /// <param name="webTitle"></param>
        /// <param name="webDesc"></param>
        /// <param name="uniquePermission"></param>
        public static void AddBlankWeb(SPWeb web, string webRelativeUrl, string webTitle, string webDesc,
                                       bool uniquePermission)
        {
            SPWeb newWeb = web.Webs.Add(webRelativeUrl, webTitle, webDesc, 1033, "STS#1", uniquePermission, false);
            newWeb.Dispose();
        }

        /// <summary>
        /// Use this method to Add a new Team web
        /// </summary>
        /// <param name="web"></param>
        /// <param name="webRelativeUrl"></param>
        /// <param name="webTitle"></param>
        /// <param name="webDesc"></param>
        /// <param name="uniquePermission"></param>
        public static void AddTeamWeb(SPWeb web, string webRelativeUrl, string webTitle, string webDesc,
                                      bool uniquePermission)
        {
            SPWeb newWeb = web.Webs.Add(webRelativeUrl, webTitle, webDesc, 1033, "STS#0", uniquePermission, false);
            newWeb.Dispose();
        }

        /// <summary>
        /// Use this method to to delete a file 
        /// </summary>
        /// <param name="web"></param>
        /// <param name="webRelativeFileUrl"></param>
        public static void DeleteFile(SPWeb web, string webRelativeFileUrl)
        {
            if (File.Exists(webRelativeFileUrl))
            {
                web.Files.Delete(webRelativeFileUrl);
            }
        }

        /// <summary>
        /// Use this method to enable some advanced settings
        /// </summary>
        /// <param name="web"></param>
        /// <param name="hideFromSearch"></param>
        public static void EnableAdvancedSettings(SPWeb web, bool hideFromSearch)
        {
            web.NoCrawl = hideFromSearch;
            web.Update();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="web"></param>
        public static void AddFile(SPWeb web)
        {
            throw new NotImplementedException("If using from feature : Use element manifest file to upload file(s).");
        }

        /// <summary>
        /// Use this method to copy the contents of the newFile into origFile to create a customized version of origFile.
        /// This can be retracted via SharePoint Designer or the SharePoint API
        /// </summary>
        /// <param name="web"></param>
        /// <param name="origFileName"></param>
        /// <param name="newFileName"></param>
        /// <param name="checkInComment"></param>
        public static void SwapFileContent(SPWeb web, string origFileName, string newFileName, string checkInComment)
        {
            SPFile origFile = web.Files[origFileName];
            SPFile newFile = web.Files[newFileName];

            // Checkout origFile
            origFile.CheckOut();

            // Copy stream from new to orig
            origFile.SaveBinary(newFile.OpenBinaryStream());

            // Checkin origFile
            origFile.CheckIn(checkInComment);
        }
    }
}